package org.springblade.modules.teambition.res;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Data;
import org.springblade.modules.teambition.config.UtcDateDeserializer;

import java.util.List;
import java.util.stream.Collectors;

@Data
public class TaskRes {
	private static  final String CUSTOMER_CFID = "675f9079355b0eb36968ea81";
	private static  final String CONTACT_NO_CFID = "675f9079355b0eb36968ea6c";
	private static final String DEVICE_TYPE_CFID = "675fa0c9f5d7852ebc603602";
	private static final String QUANTITY_CFID = "675f9079355b0eb36968ea4e";
	private static final String CUSTOM_TEXT = "text";
	private static final String CUSTOM_DELIMIT = "|";
	@JsonDeserialize(using = UtcDateDeserializer.class)
	private String accomplishTime;
	private String content;
	@JsonDeserialize(using = UtcDateDeserializer.class)
	private String created;
	private String creatorId;
	@JsonDeserialize(using = UtcDateDeserializer.class)
	private String dueDate;
	private String executorId;
	private String id;
	private String isArchived;
	private String isDone;
	private String stageId;
	private String tasklistId;
	@JsonDeserialize(using = UtcDateDeserializer.class)
	private String updated;
	private String visible;
	private String parentTaskId;
	private String note;
	@JsonDeserialize(using = UtcDateDeserializer.class)
	private String startDate;
	private String tfsId;
	private String tfsTag;
	private String taskDateRange;
	private List<CustomField> customfields;

	private String orderNo;

	private String quantity;

	private String deviceType;

	private String customer;

	private List<TaskActivityRes.ActionItem> actList;

	@Data
	private static class CustomField{
		List<CustomFieldValue> value;
		Object type;
		String cfId;
	}

	@Data
	private static class CustomFieldValue{
		String id;
		String title;
	}

	public void extractedFields(){
		this.orderNo = this.customfields.stream().filter(x-> CONTACT_NO_CFID.equals(x.getCfId()))
			.flatMap(list -> list.getValue().stream())
			.map(CustomFieldValue::getTitle).collect(Collectors.joining(CUSTOM_DELIMIT));

		this.quantity = this.customfields.stream().filter(x->QUANTITY_CFID.equals(x.getCfId()))
			.flatMap(list -> list.getValue().stream())
			.map(CustomFieldValue::getTitle).findFirst().orElse("");
		this.deviceType = this.customfields.stream().filter(x->DEVICE_TYPE_CFID.equals(x.getCfId()))
			.flatMap(list -> list.getValue().stream())
			.map(CustomFieldValue::getTitle).findFirst().orElse("");
		this.customer = this.customfields.stream().filter(x->CUSTOMER_CFID.equals(x.getCfId()))
			.flatMap(list -> list.getValue().stream())
			.map(CustomFieldValue::getTitle).findFirst().orElse("");
	}
}
